package com.ruoyi.project.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.ruoyi.project.system.domain.KnowledgeArticle;
import com.ruoyi.project.system.domain.KnowledgeCatalog;
import com.ruoyi.project.system.mapper.KnowledgeArticleMapper;
import com.ruoyi.project.system.mapper.KnowledgeCatalogMapper;
import com.ruoyi.project.system.service.IKnowledgeArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 安全常识文章Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-10-07
 */
@Service
public class KnowledgeArticleServiceImpl implements IKnowledgeArticleService
{
    @Autowired
    private KnowledgeArticleMapper knowledgeArticleMapper;


    @Autowired
    private KnowledgeCatalogMapper knowledgeCatalogMapper;

    /**
     * 查询安全常识文章
     * 
     * @param id 安全常识文章ID
     * @return 安全常识文章
     */
    @Override
    public KnowledgeArticle selectKnowledgeArticleById(Long id)
    {
        return knowledgeArticleMapper.selectKnowledgeArticleById(id);
    }

    /**
     * 查询安全常识文章列表
     * 
     * @param knowledgeArticle 安全常识文章
     * @return 安全常识文章
     */
    @Override
    public List<KnowledgeArticle> selectKnowledgeArticleList(KnowledgeArticle knowledgeArticle)
    {
        return knowledgeArticleMapper.selectKnowledgeArticleList(knowledgeArticle);
    }

    /**
     * 新增安全常识文章
     * 
     * @param knowledgeArticle 安全常识文章
     * @return 结果
     */
    @Override
    public int insertKnowledgeArticle(KnowledgeArticle knowledgeArticle)
    {

        KnowledgeCatalog knowledgeCatalog = knowledgeCatalogMapper.selectKnowledgeCatalogById(knowledgeArticle.getCatalogId().intValue());
        knowledgeArticle.setCatalogId(knowledgeCatalog.getId().longValue());
        knowledgeArticle.setCatalogTreeId(knowledgeCatalog.getIdTree());
        String[] split = knowledgeCatalog.getIdTree().toString().split(",");
        String treeName = "";
        for (String s : split) {
            KnowledgeCatalog knowledgeCatalog1 = knowledgeCatalogMapper.selectKnowledgeCatalogById(Integer.parseInt(s));
            treeName+=knowledgeCatalog1.getName()+"/";
        }
        knowledgeArticle.setCatalogTreeName(treeName.trim().substring(0,treeName.trim().lastIndexOf("/")));

        return knowledgeArticleMapper.insertKnowledgeArticle(knowledgeArticle);
    }

    /**
     * 修改安全常识文章
     * 
     * @param knowledgeArticle 安全常识文章
     * @return 结果
     */
    @Override
    public int updateKnowledgeArticle(KnowledgeArticle knowledgeArticle)
    {

        KnowledgeCatalog knowledgeCatalog = knowledgeCatalogMapper.selectKnowledgeCatalogById(knowledgeArticle.getCatalogId().intValue());
        knowledgeArticle.setCatalogId(knowledgeCatalog.getId().longValue());
        knowledgeArticle.setCatalogTreeId(knowledgeCatalog.getIdTree());
        String[] split = knowledgeCatalog.getIdTree().toString().split(",");
        String treeName = "";
        for (String s : split) {
            KnowledgeCatalog knowledgeCatalog1 = knowledgeCatalogMapper.selectKnowledgeCatalogById(Integer.parseInt(s));
            treeName+=knowledgeCatalog1.getName()+"/";
        }
        knowledgeArticle.setCatalogTreeName(treeName.trim().substring(0,treeName.trim().lastIndexOf("/")));
        return knowledgeArticleMapper.updateKnowledgeArticle(knowledgeArticle);
    }

    /**
     * 批量删除安全常识文章
     * 
     * @param ids 需要删除的安全常识文章ID
     * @return 结果
     */
    @Override
    public int deleteKnowledgeArticleByIds(Long[] ids)
    {
        return knowledgeArticleMapper.deleteKnowledgeArticleByIds(ids);
    }

    /**
     * 删除安全常识文章信息
     * 
     * @param id 安全常识文章ID
     * @return 结果
     */
    @Override
    public int deleteKnowledgeArticleById(Long id)
    {
        return knowledgeArticleMapper.deleteKnowledgeArticleById(id);
    }


    /**
     * 查询安全知识目录
     * @return
     */
    @Override
    public List<Map<String,Object>> selectKnowledgeCat() {

        List<Map<String,Object>> list = new ArrayList<>();

        List<Map<String,Object>> catList = knowledgeCatalogMapper.selectCatList();

        for (Map<String, Object> map : catList) {

                List<Map<String, Object>> chirldList = knowledgeCatalogMapper.selectCatChildList(Integer.parseInt(map.get("value").toString()));
                if(catList.size()>0){
                    map.put("children",chirldList);
                }
                list.add(map);
        }
        return list;
    }

    /**
     * 树形结构
     * 查询安全知识目录
     * @return
     */
    @Override
    public List<Map<String,Object>> selectKnowledgeCatList() {

        List<Map<String,Object>> list = new ArrayList<>();

        List<Map<String,Object>> catList = knowledgeCatalogMapper.selectCatList();

        for (Map<String, Object> map : catList) {

            List<Map<String, Object>> chirldList = childrenList(map);
            //测试若没有子数据不加children会不会报错
            if(chirldList.size()!=0 && chirldList!=null){
                map.put("children",chirldList);
            }

            list.add(map);
        }
        return list;
    }


    public List<Map<String,Object>> childrenList( Map<String,Object> map) {

        List<Map<String,Object>> list = new ArrayList<>();
        //  id as key,id as value,name as label
        //查询是否有子节点
        List<Map<String, Object>> chirldList = knowledgeCatalogMapper.selectCatChildList(Integer.parseInt(map.get("id").toString()));

        if(chirldList.size()==0 || chirldList==null){
            //List<Map<String, Object>> mapList = knowledgeArticleMapper.selectArticleList((Long) map.get("id"));
            //list.addAll(mapList);
            return list;
        }else{
            for (Map<String, Object> childrenMap : chirldList) {

                List<Map<String, Object>> maplist = childrenList(childrenMap);
                if(maplist.size()!=0 && maplist!=null){
                    childrenMap.put("children",maplist);
                }
                list.add(childrenMap);
            }
            return list;
        }
    }

    @Override
    public int updateReadCount(Long id){
        return knowledgeArticleMapper.updateReadCount(id);
    }



}
